import { baseURL } from './network.js'

//请求榜单数据
function getTopList () {
	return new Promise((resolve, reject) => {
		console.log(`${baseURL}/toplist/detail`)
		uni.request({
			url:`${baseURL}/toplist/detail`,
			success(res) {
				let newres = res.data.list
				newres.length = 4
				console.log(newres)
				resolve(newres)
			},
			fail(err) {
				uni.showToast({
					title:"网络可能存在问题...",
					icon:err
				})
				reject(err)
			}
		})
	})
	
}

//请求榜单列表
function getDetailTopList(id) { 
	return new Promise((resolve, reject) => {
		uni.request({
			url:`${baseURL}/top/list?id=` + id,
			success(res) {
				resolve(res)
			},
			fail(err) {
				uni.showToast({ 
					title:"获取榜单列表数据失败",
					icon: err
				})
				reject(err)
			}
		})
	})
}

//获取歌曲详情
function getSongDetail (songId) {
	return new Promise((resovle, reject) => {
		console.log(`${baseURL}/song/detail?ids=${songId}`)
		uni.request({
			url: `${baseURL}/song/detail?ids=${songId}`,
			success(data) {
				resovle(data)
			},
			fail(err) {
				reject(err)
			}
		})
	})
}
//获取歌词
function getLyric (songId) {
	return new Promise((resolve, reject) => {
		console.log(`${baseURL}/lyric?id=${songId}`)
		uni.request({
			url: `${baseURL}/lyric?id=${songId}`,
			success(res) {
				resolve(res)
			},
			fail(err) {
				reject(err)
			}
		})
	})
}

//获取相似音乐
function getSimilarSong (songId) {
	console.log(`${baseURL}/simi/song?id=${songId}`)
	return new Promise((resolve, reject) => {
		uni.request({
			url: `${baseURL}/simi/song?id=${songId}`,
			success(res) {
				resolve(res)
			},
			fail(err) {
				reject(err)
			}
		})
	})
}

//获取歌曲评论
function getComment(songId) {
	console.log(`${baseURL}/comment/music?id=${songId}`)
	return new Promise((resolve, reject) => {
		uni.request({
			url: `${baseURL}/comment/music?id=${songId}`,
			success(res) {
				resolve(res)
			},
			fail(err) {
				reject(err)
			}
		})
	})
}

//获取音频地址 /song/url?id=33894312
function getSongUrl(songId) {
	console.log(`${baseURL}/song/url?id=${songId}`)
	return new Promise((resolve, reject) => {
		uni.request({
			url: `${baseURL}/song/url?id=${songId}`,
			success(res) {
				resolve(res)
			},
			fail(err) {
				reject(err)
			}
		})
	})
}

//获取热搜榜
function getHotList () {
	return new Promise((resolve, reject) => {
		uni.request({
			url: `${baseURL}/search/hot/detail`,
			success(res) {
				resolve(res)
			},
			fail(err) {
				reject(err)
			}
		})
	})
}

//搜索关键词 /search?keywords= 海阔天空
function searchKeyWord (keyWord) {
	console.log(`${baseURL}/search?keywords=`+keyWord)
	return new Promise((resolve, reject) => {
		uni.request({
			url: `${baseURL}/search?keywords=`+keyWord,
			success(res) {
				resolve(res)
			},
			fail(err) {
				reject(err)
			}
		})
	})
}

module.exports = {
	getTopList,
	getDetailTopList,
	getSongDetail,
	getLyric,
	getSimilarSong,
	getComment,
	getSongUrl,
	getHotList,
	searchKeyWord,
}